
package com.ps.helpdesk.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ps.helpdesk.business.BusinessUsuario;
import com.ps.helpdesk.model.Usuario;
import com.ps.helpdesk.persistence.ManagerConnection;

/**
 * Servlet implementation class for Servlet: FormUsuario
 * 
 */
public class FormUsuario extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

	static final long serialVersionUID = 1L;

	public FormUsuario() {

		super();
		
		ManagerConnection.DATA_BASE = "helpdesk";
	}

	protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

		doPost( request, response );
	}

	protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

		if ( request.getParameter( "edit" ) != null 
				&& String.valueOf( request.getParameter( "edit" ) ).trim().length() > 0 ) {
			find( request, response );
		}
		else if ( request.getParameter( "delete" ) != null 
				&& String.valueOf( request.getParameter( "delete" ) ).trim().length() > 0 ) {
			delete( request, response );
		}
		else if ( "inserir".equals( request.getParameter( "submit" ) ) 
				|| "alterar".equals( request.getParameter( "submit" ) ) ) {
			persist( request, response );
		}
		else if ( "novo".equals( request.getParameter( "submit" ) ) ) {
			send( request, response, "?idusuario=&nomecompleto=&fone=&email=" );
		}
		else {
			send( request, response, "" );
		}
	}
	
	private void loadSituacoes( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
		
		BusinessUsuario bs = new BusinessUsuario();
		List<Usuario> usuarios = bs.findAll();

		if ( request.getAttribute( "lista" ) == null ) {
			request.setAttribute( "lista", usuarios );
		}
		else {
			((List<Usuario>)request.getAttribute( "lista" )).addAll( usuarios );
		}
	}
	
	private void send( HttpServletRequest request, HttpServletResponse response, String parameters ) throws ServletException, IOException {
		
		loadSituacoes( request, response );
		getServletContext().getRequestDispatcher( "/pages/admin/usuario.jsp" + parameters ).forward( request, response );
	}
	
	private void sendError( HttpServletRequest request, HttpServletResponse response, BusinessUsuario bs ) throws ServletException, IOException {
		
		if ( bs != null ) {
			request.setAttribute( "mensagem", bs.getMensagem() );
		}
	
		getServletContext().getRequestDispatcher( 
				"/pages/erropage.jsp?link=/helpdesk/FormUsuario" )
					.forward( request, response );
	}

	private void find( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

		BusinessUsuario bs = new BusinessUsuario();
		Usuario usuario = bs.find( request.getParameter( "edit" ) );

		if ( usuario != null ) {
			send( request, response, 
					"?idusuario=" + usuario.getIdusuario() + 
					"&nomecompleto=" + usuario.getNomecompleto() + 
					"&fone=" + usuario.getFone() + 
					"&email=" + usuario.getEmail() );
		}
		else {
			sendError( request, response, bs );
		}
	}

	private void persist( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

		BusinessUsuario bs = new BusinessUsuario();
		
		Usuario usuario = new Usuario();
		usuario.setIdusuario( request.getParameter( "idusuario" ).trim() );
		usuario.setNomecompleto( request.getParameter( "nomecompleto" ).trim() );
		usuario.setFone( request.getParameter( "fone" ).trim() );
		usuario.setEmail( request.getParameter( "email" ).trim() );
		
		if ( bs.persist( usuario ) ) {
			send( request, response, 
					"?idusuario=" + usuario.getIdusuario() + 
					"&nomecompleto=" + usuario.getNomecompleto() + 
					"&fone=" + usuario.getFone() + 
					"&email=" + usuario.getEmail() );
		}
		else {
			sendError( request, response, bs );
		}
	}

	private void delete( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

		BusinessUsuario bs = new BusinessUsuario();		
		String key = request.getParameter( "delete" ).trim();
		
		if ( bs.remove( key ) ) {
			send( request, response, "?idusuario=&nomecompleto=&fone=&email=" );
		}
		else {
			sendError( request, response, bs );
		}
	}
}